<?php

/*

Process Name Caught Pets (searching_caught_pet.pro.php)

*/
ob_start();
include "global.inc.php";

$findSearch = fetch("SELECT * FROM search_pets2 WHERE userid = '$userid' AND game = '$game'");
if (!$findSearch[id])
{
	die(header(error("search_for_pets.php?game=$game","You are not searching right now.")));
}

if ($findSearch[found] != "caught_pet")
{
	die(header(error("searching_for_pets.php?game=$game","You have not caught a pet yet.")));
}

$findPet = fetch("SELECT * FROM pets2 WHERE name = '$findSearch[found_what]' AND game = '$game'");

$check = fetch("SELECT * FROM user_pets2 WHERE name = '$petname' AND game = '$game'");
if ($check[name])
{
	die(header(error("searching_for_pets.php?game=$game&net=$net","There is already a pet with that name. Try another name.")));
}
if ($petname == "")
{
	die(header(error("searching_for_pets.php?game=$game&net=$net","Don't leave the name blank.")));
}
if (preg_match('/^[a-zA-Z0-9_]*$/UD',$petname))
{
	$randStrength = rand(1,3) * $level;
	$randIntelligence = rand(1,3) * $level;
	$randSpeed = rand(1,3) * $level;
	$randDefense = rand(1,3) * $level;
	$randHP = rand(1,6) * $level;
	$plusDefault = rand(2,8);

	$level = $findSearch[found_what2];
	$strength = $randStrength + $plusDefault;
	$intelligence = $randIntelligence + $plusDefault;
	$speed = $randSpeed + $plusDefault;
	$defense = $randDefense + $plusDefault;
	$health = $randHP + 100;

	mysql_query("INSERT INTO user_pets2 (name,species,owner,level,strength,intelligence,speed,hunger,timestamp,defense,current_hp,max_hp,game) VALUES ('$petname','$findPet[id]','$userid','$level','$strength','$intelligence','$speed','7','$timestamp','$defense','$health','$health','$game')");

	mysql_query("UPDATE search_pets2 SET found = '' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET found_what = '' WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET found_what2=0 WHERE userid = '$userid' AND game = '$game'");
	mysql_query("UPDATE search_pets2 SET chance_of_running=30 WHERE userid = '$userid' AND game = '$game'");

	die(header(error("searching_for_pets.php?game=$game","Congratulations on catching your new $findPet[name] named $petname!")));
}
else
{
	die(header(error("searching_for_pets.php?game=$game&net=$net","Your pet's name can only contain a-z, A-Z, 0-9 and underscores. Please try again.")));
}

header("Location: searching_for_pets.php?game=$game");
?>